package com.itheima.manage.controller;

import cn.hutool.core.util.StrUtil;
import com.itheima.commons.vo.backstage.*;
import com.itheima.manage.service.ManageService;
import com.itheima.manage.service.ManageUserService;
import com.itheima.manage.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 管理控制器
 *
 * @author yaohu
 * @date 2021/12/16
 * @Version 1.0
 * @describe
 */
@RestController
@RequestMapping("/manage")
public class ManageController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private ManageService manageService;

    @Autowired
    ManageUserService manageUserService;

    /**
     * 消息翻页
     *
     * @param pagesize  页尺寸
     * @param page      页数
     * @param sortProp  排序字段
     * @param sortOrder ascending 升序 descending 降序
     * @param likeId    消息id
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @param userId    用户id
     * @param state     审核状态
     * @param //param   userID 管理员id
     *                  reasonsForThawing 解冻原因
     * @return
     */
    @GetMapping("/messages")
    public ResponseEntity messages(
            @RequestParam(value = "pagesize", required = false, defaultValue = "10") Integer pagesize,
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "sortProp", defaultValue = "createDate") String sortProp,
            @RequestParam(value = "sortOrder", defaultValue = "descending") String sortOrder,
            @RequestParam(value = "id", required = false) Integer likeId,
            @RequestParam(value = "sd", required = false, defaultValue = "-1") String startDate,
            @RequestParam(value = "ed", required = false, defaultValue = "-1") String endDate,
            @RequestParam(value = "uid", required = false) String userId,
            @RequestParam(value = "state", defaultValue = "3") String state,
            @RequestBody(required = false) Map<String, Object> param) {
        PageResult pageResult;
        // 判断是否为详情展示
        if (StrUtil.isNotBlank(userId)) {
            pageResult = messageService.passPublishList(page, pagesize, sortProp, sortOrder, userId);
        } else {
            // 审核列表
            pageResult = messageService.messages(pagesize, page, likeId, startDate, endDate, state);
        }
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 生成tb_publish_info内容
     *
     * @return
     */
    @PostMapping("/messages/create")
    public ResponseEntity messageCreate() {
        messageService.messageCreate();
        return ResponseEntity.ok(null);
    }

    /**
     * 消息通过
     *
     * @param ids 审核id
     * @return
     */
    @PostMapping("/messages/pass")
    public ResponseEntity messagePass(@RequestBody List<String> ids) {
        Boolean pass = messageService.messagesPass(ids);
        if (pass) {
            return ResponseEntity.ok("操作成功");
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 消息拒绝
     *
     * @param ids 审核id
     * @return
     */
    @PostMapping("/messages/reject")
    public ResponseEntity messageReject(@RequestBody List<String> ids) {
        Boolean reject = messageService.messagesReject(ids);
        if (reject) {
            return ResponseEntity.ok("操作成功");
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 审核撤销
     *
     * @param ids 审核id
     * @return
     */
    @PostMapping("/messages/revocation")
    public ResponseEntity messageRevocation(@RequestBody List<String> ids) {
        Boolean reject = messageService.messagesRevocation(ids);
        if (reject) {
            return ResponseEntity.ok("操作成功");
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 消息详情
     *
     * @param publishInfoId
     * @return
     */
    @GetMapping("/messages/{id}")
    public ResponseEntity messageDetails(@PathVariable(value = "id") String publishInfoId) {
        MessageVo messageVo = messageService.messageDetails(publishInfoId);
        return ResponseEntity.ok(messageVo);
    }

    /**
     * 消息置顶
     *
     * @param publishInfoId
     * @return
     */
    @PostMapping("/messages/{id}/top")
    public ResponseEntity messageTop(@PathVariable(value = "id") String publishInfoId) {
        Boolean top = messageService.messageTop(publishInfoId);
        if (top) {
            return ResponseEntity.ok("操作成功");
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 消息取消置顶
     *
     * @param publishInfoId
     * @return
     */
    @PostMapping("/messages/{id}/untop")
    public ResponseEntity messageUnTop(@PathVariable(value = "id") String publishInfoId) {
        Boolean unTop = messageService.messageUnTop(publishInfoId);
        if (unTop) {
            return ResponseEntity.ok("操作成功");
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 查询人工审核日志
     *
     * @param page      当前页
     * @param pagesize  每页多少条
     * @param username  管理员姓名
     * @param operation 操作内容
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @GetMapping("/check/log")
    public ResponseEntity findCheckLog(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "pagesize", required = false, defaultValue = "10") Integer pagesize,
            @RequestParam(value = "username", required = false) String username,
            @RequestParam(value = "operation", required = false) String operation,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime) {
        PageResult pageResult = messageService.findCheckLog(page, pagesize, username, operation, startTime, endTime);
        return ResponseEntity.ok(pageResult);
    }
    /*//获取评论列表
    @GetMapping("/messages/comments")
    public ResponseEntity CommentList(@RequestBody Map<String, Object> map,
                                      @RequestParam("pagesize") String pagesize,
                                      @RequestParam("page") String page,
                                      @RequestParam("sortProp") String sortProp,
                                      @RequestParam("sortOrder") String sortOrder,
                                      @RequestParam("messageID") String messageID) {
        CommentBlackVo commectList = manageService.getCommectList(map, pagesize, page, sortProp, sortOrder, messageID);
        return ResponseEntity.ok(commectList);
    }*/

    //数据统计之注册存留

    @GetMapping("/Statistics/Keep")
    public ResponseEntity keeps(
            @RequestParam(value = "datebefore", required = false) Date after,
            @RequestParam(value = "dateafter", required = false) Date before,
            @RequestParam(value = "sex", required = false) String sex) {
        List<KeepVo> keepVoList = manageService.getkeeps(after, before, sex);
        return ResponseEntity.ok(keepVoList);
    }

    /**
     * 列表查询
     * 按条件查询（默认查询结果显示按照最后活跃时间倒序）
     * 1.按id模糊查询
     * 2.按昵称模糊查询
     * 3.按城市 下拉菜单选择省份
     * <p>
     * 用户管理/用户信息列表
     * <p>
     * pagesize   显示条数
     * page       当前页
     * id         用户id
     * nickname  昵称
     * city      注册城市
     *
     * @return
     */
    @GetMapping("/users")
    public ResponseEntity queryUserInfoByIdAndNameAndCity(

            @RequestParam(value = "id", required = false) String id,
            @RequestParam(value = "nickname", required = false) String nickname,
            @RequestParam(value = "city", required = false) String city,
            @RequestParam(value = "page", defaultValue = "1", required = false) Long page,
            @RequestParam(value = "pagesize", defaultValue = "20", required = false) Long pagesize
    ) {
        PageResult pageResult = manageUserService.queryUserInfoByIdAndNameAndCity(id, nickname, city, page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 根据用户id查询当前用户详细信息
     *
     * @param userId 用户id
     * @return
     */
    @GetMapping("/users/{userID}")
    public ResponseEntity<UserInfoMapVo> queryById(@PathVariable("userID") String userId) {
        UserInfoMapVo userInfoMpVo = manageUserService.queryById(userId);
        return ResponseEntity.ok(userInfoMpVo);
    }

    /**
     * 冻结用户
     * <p>
     * userid             用户id
     * freezingTime       冻结时间
     * freezingRange        冻结范围
     * reasonsForFreezing  冻结原因
     * frozenRemarks       冻结备注
     *
     * @param params
     * @return
     */
    @PostMapping("/users/freeze")
    public ResponseEntity freeze(@RequestBody Map params) {
        Map map = manageUserService.userFreeze(params);
        return ResponseEntity.ok(map);
    }

    /**
     * 解冻用户
     * <p>
     * UID                 用户ID
     * reasonsForThawing  解冻原因
     *
     * @param params
     * @return
     */
    @PostMapping("/users/unfreeze")
    public ResponseEntity unfreeze(@RequestBody Map params) {
        Map map = manageUserService.userUnfreeze(params);
        return ResponseEntity.ok(map);
    }


    /**
     * 查询指定用户发布的所有视频列表
     *
     * @param page      当前页
     * @param pagesize  显示条数 默认最多10条
     * @param uid       当前查看的用户的id
     * @param sortProp  排序字段
     * @param sortOrder 排序方式 desc 降序   asce 升序
     * @return
     */
    @GetMapping("/videos")
    public ResponseEntity videos(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer pagesize,
                                 @RequestParam Long uid,
                                 @RequestParam String sortProp,
                                 @RequestParam String sortOrder) {
        PageResults results = manageUserService.findAllVideos(page, pagesize, uid, sortProp, sortOrder);
        return ResponseEntity.ok(results);
    }


    /**
     * 查询登录记录
     *
     * @param uid 动态id
     * @return
     */
    @GetMapping("/logs")
    public ResponseEntity logs(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize,
            @RequestParam Long uid,
            @RequestParam String sortProp,
            @RequestParam String sortOrder) {

        PageResults pageResults = manageUserService.queryLogById(uid, page, pagesize, sortProp, sortOrder);
        System.out.println(pageResults);
        return ResponseEntity.ok(pageResults);
    }

    /**
     * 评论列表翻页
     */
    @GetMapping("/messages/comments")
    public ResponseEntity comments(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   @RequestParam String messageID,
                                   @RequestParam String sortProp,
                                   @RequestParam String sortOrder
    ) {
        PageResult pageResult = manageUserService.comments(page, pagesize, messageID, sortProp, sortOrder);

        return ResponseEntity.ok(pageResult);
    }

    /**
     * 动态置顶
     *
     * @param movementId 动态id
     * @return
     */
    /*
    @GetMapping("/messages/{id}/top")
    public ResponseEntity top(@PathVariable("id") String movementId) {
        manageUserService.movementTop(movementId);
        return ResponseEntity.ok("操作成功");
    }*/
}
